package com.polito.mad.bikeponyapp.fragments;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.polito.mad.bikeponyapp.R;
import com.polito.mad.bikeponyapp.activity.FeedbackActivity;
import com.polito.mad.bikeponyapp.util.SignatureSurfaceView;
import com.polito.mad.bikeponyapp.util.SignatureSurfaceView.OnSignatureListener;

public class SignatureFragment extends Fragment  implements OnSignatureListener{
	
		
	private OnSignatureSaveListener mSigSaveListner;
	
	private Activity parentActivity  ;
	
	private SignatureSurfaceView myDrawingPanel ;
	
	private Button saveButton;
	private Button clearButton;
	
	private byte[] byteImage;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    parentActivity  = getActivity();
	    getActivity().getActionBar().setDisplayShowTitleEnabled(true);
	    getActivity().setTitle("Delivery signature");
	    if(savedInstanceState != null)
	    {
	    	byteImage = savedInstanceState.getByteArray("signagureImageBytes");
	    }
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		
		myDrawingPanel = (SignatureSurfaceView)getView().findViewById(R.id.view_signature);
		myDrawingPanel.setOnSignatureListener(this);
		if(parentActivity instanceof FeedbackActivity)
		{
			/*
			byte[] sigArray = ((FeedbackActivity)parentActivity).getSignatureArray();
			if(sigArray != null)
			{
				myDrawingPanel.loadSurface(sigArray);
			}
			*/
		}
	};
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
		      Bundle savedInstanceState) {
	    
		View myView = inflater.inflate(R.layout.signature_fragment_layout, container, false);
		
		clearButton = (Button) myView.findViewById(R.id.clearSignatureButton);
		clearButton.setEnabled(false);
		clearButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				clearSignature();
			}
		});
		
		saveButton = (Button) myView.findViewById(R.id.saveSignatureButton);
		saveButton.setEnabled(false);
		saveButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				saveImage();
			}
		});
		
		return myView;
	}
	
	private void saveImage()
	{
		byte[] myArray = myDrawingPanel.getImageByteArray();
		if( myArray != null)
			mSigSaveListner.onSignatureSave(myArray);//send update to activity
		else
			System.out.println("image not found");//TODO throw proper array
	}
	
	// Container Activity must implement this interface
    public interface OnSignatureSaveListener {
        public void onSignatureSave(byte[] image);
    }
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
        	mSigSaveListner = (OnSignatureSaveListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnSignatureSaveListener");
        }
    }
    
    
    @Override
    public void onStart() {
    	super.onStart();
    	//showToolbar();
    }
		
	private void clearSignature()
	{
		myDrawingPanel.clearSignature();
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);

	}
	
	 public void showToolbar()
     {
         RelativeLayout layout = (RelativeLayout)getView().findViewById(R.id.feeback_fragment_toolbar_Layout);
         layout.setVisibility(View.VISIBLE);
         Animation bump = AnimationUtils.loadAnimation(getActivity(), R.anim.bump_out_view);
         layout.setAnimation(bump);
         bump.setAnimationListener(new Animation.AnimationListener() {
                 
                 @Override
                 public void onAnimationStart(Animation animation) {
                         // TODO Auto-generated method stub
                	 
                 }
                 
                 @Override
                 public void onAnimationRepeat(Animation animation) {
                         // TODO Auto-generated method stub
                 }
                 
                 @Override
                 public void onAnimationEnd(Animation animation) {
                         getView().invalidate();
                         
                 }
         });
         bump.setFillAfter(true);
         bump.start();
     }

	@Override
	public void onSaveInstanceState(Bundle outState) {
		if(myDrawingPanel != null)
		{
			if(parentActivity instanceof FeedbackActivity)
			{
				((FeedbackActivity)parentActivity).setSignatureArray(myDrawingPanel.getImageByteArray());
				
				
			}
			//outState.putByteArray("signagureImageBytes", myDrawingPanel.getImageByteArray());
			
			outState.putByteArray("signagureImageBytes", myDrawingPanel.getImageByteArray());
		}
		
		

		
		super.onSaveInstanceState(outState);
		
		
	}
	
	public byte[] getSignatureByteArray()
	{
		if(myDrawingPanel != null)
			return myDrawingPanel.getImageByteArray();
		return null;
	}
	
	@Override
	public void onPause() {
		super.onPause();
		
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if(myDrawingPanel != null)
		{
			myDrawingPanel.setImageByteArray(byteImage);
		}
		//SharedPreferences settings = getSharedPreferences("canvas_data", 0);
	}

	@Override
	public void startSigning() {
		clearButton.setEnabled(true);
		saveButton.setEnabled(true);
	}

	@Override
	public void stopSigning() {
		clearButton.setEnabled(false);
		saveButton.setEnabled(false);
	}
	
}
